var itemId = 0;
var summarySort = 1;

$(function(){
	//拖拽排序
	$('.sortDiv').sortable();

	//判断按钮的显示隐藏 
	/*if($('.xxlBlock .xlBlock').length < 2){
		$('.xxlBlock .xlBlock:first .sortBtnW img:first').hide();
	}

	if($('.xxlBlock .xlBlock').length >= 2){
		$('.xxlBlock .xlBlock:first .sortBtnW img:first').hide();
		$('.xxlBlock .xlBlock:last .sortBtnW img:last').hide();
	}*/

	//添加一个小节
	$('#addBlock').on('click',function(){
		itemId--;
		summarySort++;

		var bigBlock = $('<div class="xlBlock" style="position:relative;"></div>');
		var controlGroup = $('<input type="hidden" id="itemSummarySort'+itemId+'" value="'+summarySort+'"/><div class="control-group"></div>');
		var label = $('<label class="control-label" for="inputEmail"></label>').text('');
		var controls = $('<div  class="controls"></div>');
		var select = $('<select id="itemTargetType'+itemId+'"><option value="0">景点</option><option value="1">商家</option><option value="2">商品</option></select>');
		var input1 = $('<input type="text" class="input-xlarge" id="itemTargetId'+itemId+'" placeholder="请输入ID"/>');
		var input1Span = $('<span class="help-inline renderSellerId"></span>');
		var input2 = $('<input type="text" class="input-medium" id="itemRemarks'+itemId+'" placeholder="备注"/>');
		var input3 = $('<input type="file" id="pic'+itemId+'" name="pic"><input type="button" class="btn btn-primary" value="上传本小节图片" onclick="uploadItemPic('+itemId+');"/>');
		var input4 = $('<input type="button" class="btn btn-primary" value="删除本小节"/>');
		var input5 = $('<input type="button" class="btn btn-primary" value="保存本小节" onclick="saveOrUpdateItem('+itemId+');"/>');

		controls.append(select);
		controls.append(input1);
		controls.append(input1Span);
		controls.append(input2);
		controls.append(input3);
		controls.append(input4);
		controls.append(input5);

		controlGroup.append(label);
		controlGroup.append(controls);
		

		var controlGroup2 = $("<div class='control-group'></div>");
		var label2 = $('<label class="control-label" for="inputEmail"></label>').text('榜单中的一句话描述：');
		var controls2 = $('<div  class="controls"></div>');
		var controls2Input2 = $('<input type="text" class="input-large" id="itemDescription'+itemId+'"/>');

		controls2.append(controls2Input2);
		controlGroup2.append(label2);
		controlGroup2.append(controls2);


		var block3 = $('<input type="hidden" id="itemImageIds'+itemId+'"/><div class="Block"></div>');
		var controlGroup3 = $("<div class='control-group'></div>");
		var controls3 = $('<div class="controls sortDiv" style="overflow:hidden;" id="itemImages'+itemId+'"></div>');
		var controls3Div = $('<div style="float:left;margin-left:10px;position:relative;"></div>');
		var divImg = $('<img src="fawfw" style="display:block;width:224px;margin-bottom:10px;" class="img-rounded"/>');
		var divTextarea = $('<textarea rows="5" class="input-large" placeholder="输入描述文字"></textarea>');
		var divStrong = $('<strong style="display:block;font-size:25px;position:absolute;top:10px;left:200px;">X</strong>');


		controls3Div.append(divImg);
		controls3Div.append(divTextarea);
		controls3Div.append(divStrong);
		controls3.append(controls3Div);
		controlGroup3.append(controls3);
		block3.append(controlGroup3);

		var sortBtnW = $('<div class="sortBtnW" style="position:absolute;top:50%;right:12%;margin-top:-17px;">');
		var img1 = $('<img src="../../../images/cms/top.jpg" style="width:30px;display:block;cursor:pointer;" class="topXlBlock"/>');
		var img2 = $('<img src="../../../images/cms/bottom.jpg" style="width:30px;cursor:pointer;" class="bottomXlBlock"/>');
		sortBtnW.append(img1);
		sortBtnW.append(img2);


		bigBlock.append('<ul class="nav nav-list"><li class="divider"></li></ul>');
		bigBlock.append(controlGroup);
		bigBlock.append(controlGroup2);
		bigBlock.append(block3);
		bigBlock.append(sortBtnW);
		
		$('.xxlBlock').append(bigBlock);
	});
	
	
	//点击排序向上
	$('.xxlBlock').delegate('.topXlBlock','click',function(){
		var _that = $(this);
		_that.parents('.xlBlock').prev().insertAfter(_that.parents('.xlBlock'));

	});

	//点击排序向下
	$('.xxlBlock').delegate('.bottomXlBlock','click',function(){alert('!!!');
		var _that = $(this);
		_that.parents('.xlBlock').next().insertBefore(_that.parents('.xlBlock'));
		//to do flag
		/*$('.xxlBlock .xlBlock:first .sortBtnW img').show();
		$('.xxlBlock .xlBlock:last .sortBtnW img').show();

		if($('.xxlBlock .xlBlock').length < 2){
			$('.xxlBlock .xlBlock:first .sortBtnW img:first').hide();
			$('.xxlBlock .xlBlock:last .sortBtnW img:last').show();
		}

		if($('.xxlBlock .xlBlock').length >= 2){
			$('.xxlBlock .xlBlock:first .sortBtnW img:first').hide();
			$('.xxlBlock .xlBlock:first .sortBtnW img:last').show();

			$('.xxlBlock .xlBlock:last .sortBtnW img:last').hide();
			$('.xxlBlock .xlBlock:last .sortBtnW img:first').show();
		}*/
	})


	function strategyFile(uploadBtn,uploadC,flag){
		
  		var uploadButton = $("#"+uploadBtn);
		var iconRoot = $("#uploadImgList") ;
		
		var doUploadButt = iconRoot.find(".upload-butt") ;
		
		iconRoot.find(".cls").on("click",function(){
			$(this).parent().fadeOut();
		}) ;
		
  		var imageUploader = new plupload.Uploader({
		    browse_button: uploadBtn ,
		    url:testJybbHost + "/jybb-upload/image2/upload.do"
		}) ;
  		
  		imageUploader.init() ;
		
  		
  		imageUploader.bind('FilesAdded', function(up, files){
  			iconRoot.find("ol").empty() ;
			doUploadButt.show() ;
			iconRoot.show("slow") ;

		    plupload.each(files, function(file) {
		         var li = $("<li style='margin-bottom:10px;'></li>").attr("id",file.id).text(file.name + ' ' + plupload.formatSize(file.size)).appendTo(iconRoot.find('ol'));
		        var delSpan = $("<span style='float:right;margin-right:20px;color:red;cursor:pointer;'>X</span>")
		        .appendTo(li)
		        .on("click",function(){
		        	$(this).parent().remove() ;
		        	if(!iconRoot.find("ol > li").size()){
		        		doUploadButt.hide() ;
		        		iconRoot.hide() ;
		        	}
		        }) ;
		    });
		});

  		/*imageUploader.bind('Uploa   dProgress', function(up, file) {
            $('#' + file.id + ' > span').text(file.percent);
		});*/
  		
  		imageUploader.bind('FileUploaded', function(up, file,result) {
            var resp = JSON.parse(result.response);
  			var imgId = resp.result[0].imageId;
            var imgUrl = resp.result[0].imageUrl;
            
  		});

        imageUploader.bind('UploadComplete', function() {
            iconRoot.hide() ;
            alert('上传成功');
        });
  		
  		imageUploader.bind('Error', function(up,err) {
		    alert("文件上传发生错误，请重试!") ;
		});
  		
  		uploadButton.on("click",function(){
			doUploadButt.on("click",function(){
				imageUploader.start() ;
				$(this).unbind("click") ;
			}) ;
		
		}) ;
  		
	};

	strategyFile('uploadSellerImgBtn',"#uploadSellerImgC","4");

});


